﻿/**********************************************************************************************
 * Copyright 2009 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file 
 * except in compliance with the License. A copy of the License is located at
 *
 *       http://aws.amazon.com/apache2.0/
 *
 * or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under the License. 
 *
 * ********************************************************************************************
 *
 *  Amazon Product Advertising API
 *  Signed Requests Sample Code
 *
 *  API Version: 2009-03-31
 *
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace AmazonProductAdvtApi
{
    class ItemLookupSample
    {
        private const string MY_AWS_ACCESS_KEY_ID = "AKIAIX3KSQ7WGRFPAOGA";
        private const string MY_AWS_SECRET_KEY = "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";
        private const string DESTINATION          = "ecs.amazonaws.com";

        private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2011-08-01";

        public static void Main()
        {
            SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);
            String requestUrl;

            /*
             * Here is an ItemLookup example where the request is stored as a dictionary.
             */
            IDictionary<string, string> r1 = new Dictionary<string, String>();
            r1["Service"] = "AWSECommerceService";
            r1["Version"] = "2011-08-01";
            r1["Operation"] = "ItemSearch";
            r1["AWSAccessKeyId"] = "AKIAIX3KSQ7WGRFPAOGA";
            r1["AssociateTag"] = "PutYourAssociateTagHere";
            r1["SearchIndex"] = "Books";
            r1["ItemPage"] = "1";
            r1["Keywords"] = "harry potter";

            requestUrl = helper.Sign(r1);

            System.Console.WriteLine("First 10 item from keyword: harry potter:");
            System.Console.WriteLine();

            FetchTitle(requestUrl);

            System.Console.WriteLine("Hit Enter to end");
            System.Console.ReadLine();
        }

        private static void FetchTitle(string url)
        {
            try
            {
                WebRequest request = HttpWebRequest.Create(url);
                WebResponse response = request.GetResponse();
                XmlDocument doc = new XmlDocument();
                doc.Load(response.GetResponseStream());

                XmlNodeList errorMessageNodes = doc.GetElementsByTagName("Message", NAMESPACE);
                if (errorMessageNodes != null && errorMessageNodes.Count > 0)
                {
                    String message = errorMessageNodes.Item(0).InnerText;
                    System.Console.WriteLine("Error: " + message + " (but signature worked)");
                }

                XmlNodeList titleNodes = doc.GetElementsByTagName("Title");
                foreach(XmlNode titles in titleNodes)
                {
                    if(titles != null)
                    {
                        System.Console.WriteLine("Title is \"" + titles.InnerXml + "\"");
                        System.Console.WriteLine();
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Caught Exception: " + e.Message);
                System.Console.WriteLine("Stack Trace: " + e.StackTrace);
            }
        }
    }
}
